﻿/*
Copyright (c) 2013, Marc Tanti
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the www.marctanti.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MARIA.level;
using MARIA.program.statementfactory;
using MARIA.program.statement;

namespace MARIA
{
    public partial class StatementsListControl : UserControl
    {

        private Level level;
        private ListViewItem lastSelectedItem;
        
        public StatementsListControl()
        {
            InitializeComponent();

            ImageList imageList = new ImageList();
            foreach (StatementFactory statement in Cache.Instance.Statements)
            {
                imageList.Images.Add(statement.StatementTextualRepresentation, statement.StatementImage);
            }
            lstStatements.LargeImageList = imageList;
        }

        public void SetLevel(Level level)
        {
            this.level = level;
            lstStatements.Items.Clear();
            foreach (StatementFactory statement in level.StatementList)
            {
                ListViewItem item = new ListViewItem(statement.StatementTextualRepresentation, statement.StatementTextualRepresentation);
                item.ToolTipText = statement.StatementDescription;
                lstStatements.Items.Add(item);
            }
            SetSelectedIndex(0);
        }

        private StatementFactory ItemToStatement(ListViewItem item)
        {
            int index = lstStatements.Items.IndexOf(item);
            if (index != -1)
            {
                if (index < level.StatementList.Length)
                {
                    return level.StatementList[index];
                }
                else
                {
                    return ClipboardFactory.Instance;
                }
            }
            else
            {
                return null;
            }
        }

        private ListViewItem StatementToItem(StatementFactory statement)
        {
            int index = Array.IndexOf(level.StatementList, statement);
            if (index != -1)
            {
                return lstStatements.Items[index];
            }
            else
            {
                if (statement is ClipboardFactory && IsClipboardExists())
                {
                    return lstStatements.Items[lstStatements.Items.Count - 1];
                }
                else
                {
                    return null;
                }
            }
        }

        public int GetSelectedIndex()
        {
            if (lstStatements.SelectedIndices.Count == 0)
            {
                return -1;
            }
            else
            {
                return lstStatements.SelectedIndices[0];
            }
        }

        private ListViewItem GetSelectedItem()
        {
            if (lstStatements.SelectedIndices.Count == 0)
            {
                return null;
            }
            else
            {
                return lstStatements.Items[GetSelectedIndex()];
            }
        }

        public StatementFactory GetSelectedStatement()
        {
            if (lstStatements.SelectedIndices.Count == 0)
            {
                return null;
            }
            else
            {
                return ItemToStatement(GetSelectedItem());
            }
        }

        public void SetSelectedIndex(int index)
        {
            lstStatements.SelectedIndices.Clear();
            lstStatements.SelectedIndices.Add(index);
        }

        private void SetSelectedItem(ListViewItem item)
        {
            SetSelectedIndex(lstStatements.Items.IndexOf(item));
        }

        public void SetSelectedStatement(StatementFactory statement)
        {
            SetSelectedItem(StatementToItem(statement));
        }

        public void MakeUneditable()
        {
            lstStatements.Enabled = false;
        }

        public void MakeEditable()
        {
            lstStatements.Enabled = true;
        }

        public void PutClipboard()
        {
            ClearClipboard();
            ListViewItem item = new ListViewItem(ClipboardFactory.Instance.StatementTextualRepresentation, ClipboardFactory.Instance.StatementTextualRepresentation);
            item.ToolTipText = ClipboardFactory.Instance.StatementDescription;
            lstStatements.Items.Add(item);
        }

        public bool IsClipboardSelected()
        {
            return GetSelectedIndex() == level.StatementList.Length;
        }

        public bool IsClipboardExists()
        {
            return lstStatements.Items.Count == level.StatementList.Length + 1;
        }

        public void ClearClipboard()
        {
            if (IsClipboardExists())
            {
                lstStatements.Items.RemoveAt(lstStatements.Items.Count - 1);
                SetSelectedIndex(lstStatements.Items.Count - 1);
            }
        }

        private void lstStatements_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstStatements.SelectedIndices.Count == 1)
            {
                if (lastSelectedItem != null)
                {
                    lastSelectedItem.BackColor = Color.Transparent;
                    lastSelectedItem.ForeColor = Color.Black;
                }

                ListViewItem selectedItem = GetSelectedItem();
                selectedItem.BackColor = Color.Blue;
                selectedItem.ForeColor = Color.White;

                lastSelectedItem = selectedItem;
            }
        }

        private void lstStatements_ItemDrag(object sender, ItemDragEventArgs e)
        {
            Statement statement = ItemToStatement((ListViewItem)e.Item).MakeInstance();
            DoDragDrop(new DragProxy(statement), DragDropEffects.All);
        }

        private void lstStatements_MouseUp(object sender, MouseEventArgs e)
        {
            if (GetSelectedItem() == null)
            {
                if (lastSelectedItem != null)
                {
                    SetSelectedItem(lastSelectedItem);
                }
                else
                {
                    SetSelectedIndex(0);
                }
            }
        }

    }
}
